Type CGObjs Extends CUI

	Field nexty:Int = 0

	Method init(p:CUI=Null)
		Local gads:TGadget[10]
		window = CreateWindow("Object Manager", 10, 10, 256, 512,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )
		gads[0] = CreateLabel("Objects List",5,5,ClientWidth(window)-10,20,window)
		nexty = 5+GadgetHeight(gads[0])
		gads[1] = CreateListBox(5,nexty,ClientWidth(window)-10,200,window)
		nexty = nexty+5+GadgetHeight(gads[1])
		gads[2] = CreateButton("Create",5,nexty,(ClientWidth(window)/2)-3,30,window)
		gads[3] = CreateButton("Remove",5+ClientWidth(window)/2,nexty,(ClientWidth(window)/2)-3,30,window)
		nexty = nexty+5+GadgetHeight(gads[3])
		gads[4] = CreateButton("Add to Map",5,nexty,ClientWidth(window)-5,30,window)

		gadgets = gads
		RefreshObjects()
		Super.Init(p)
		
	EndMethod
	
	
	Method Update(EID:Int)

		Select EID
			Case EVENT_GADGETACTION
				Select EventSource()
				
					Case gadgets[2]

						Local filter:String="B3d Files:b3d"
						Local filename:String=RequestFile( "Select B3d file to open",filter )
						If filename <> "" Then
							Local result:Int = AddObject(filename)
							If result = 1 Then
								RefreshObjects()
							EndIf
						EndIf
				
					Case gadgets[4]		
						If SelectedGadgetItem(gadgets[1]) <> -1 Then
							AddtoMap(SelectedGadgetItem(gadgets[1]))
							
						
						EndIf
				
				EndSelect
				
			Case EVENT_WINDOWCLOSE
				Select EventSource()

		
					Case window
						If Parent <> Null Then
							'ListRemove(Parent.Children,Self)
							
						EndIf	
						'FreeGadget(window)
						HideGadget(window)
				EndSelect

		
		
		End Select
		Super.Update(EID)
	EndMethod

	Method AddtoMap:Int(item:Int)
	
		Local doc:xmlDocument = xmlDocument(GadgetItemExtra(gadgets[1],item))
		If doc = Null Then Return 0
		Local root:xmlNode = doc.Root()
		If root.HasChildren() Then
			Local go:xmlNode = root.FirstChild()
			Select Lower(go.Name)
			
				Case "gameobject"
					Local tfile:String = go.Attribute("filename").Value
					If tfile = "" Then Return 0
					Local mo:MapObject = New MapObject
					mo.Mesh = LoadMesh(WorkingDir+"\models\"+tfile)
					FlipMesh(TMesh(mo.Mesh))
					mo.Node = go.CopyTo(CURRENTMAP.Root(), AS_CHILD)
					mo.Node.Attribute("type").Value = "mesh"
					mo.name = StripExt(StripDir(tfile))
					ListAddLast(MapObjects,mo)
					MainEditor.MainUI.RefreshSceneList()
			EndSelect
		Else
			Return 0
		EndIf
		
		
	EndMethod

	Method RefreshObjects()
	
		ClearGadgetItems(gadgets[1])
		Local dir:Int=ReadDir(WorkingDir+"\models")
		
		If Not dir RuntimeError "failed to read current directory"
		
		Repeat
			Local t:String=NextFile( dir )
			If t="" Exit
			If t="." Or t=".." Continue
			If Lower(ExtractExt(t)) = "xml" Then
				Local doc:xmlDocument = New xmlDocument
				doc.Load(WorkingDir+"\models\"+t)
				AddGadgetItem(gadgets[1],StripExt(StripDir(t)),0,-1,"",doc)
			EndIf
		Forever
		
		CloseDir dir		
	
	EndMethod

	Method AddObject:Int(file:String)
	
		If file = "" Then Return 0
		If FileType(WorkingDir+"\models\"+StripDir(file)) = 1 Then
			Local result:Int=Confirm(StripDir(file)+"already exists in working directory, Replace?")
			Select result
				Case 0
					Return 0
				Case 1
					CopyFile(file,WorkingDir+"\models\"+StripDir(file))
			EndSelect
		Else
			CopyFile(file,WorkingDir+"\models\"+StripDir(file))
		EndIf
		Local doc:xmlDocument = New xmlDocument
		Local root:xmlNode = doc.Root()
		root.Name = "Config"
		Local go:xmlNode = root.AddNode("GameObject", AS_CHILD)
		go.Attribute("filename").Value = StripDir(file)
		go.Attribute("name").Value = StripExt(StripDir(file))
		doc.Save(WorkingDir+"\models\"+StripExt(StripDir(file))+".xml")
		Return 1
	EndMethod
	
	
EndType
